package com.task.web.controller.core;

import javax.annotation.Resource;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.task.common.annotation.Log;
import com.task.common.core.controller.BaseController;
import com.task.common.core.domain.AjaxResult;
import com.task.common.core.page.TableDataInfo;
import com.task.common.enums.BusinessType;
import com.task.common.utils.SecurityUtils;
import com.task.core.dto.request.EngineRequestDTO;
import com.task.core.service.CheckService;
import com.task.core.service.EngineService;

import io.swagger.annotations.Api;

/**
 * @author 迪迦.
 * @date 2024/11/4 14:30
 */
@Api("")
@RestController
@RequestMapping("/engine")
public class EngineController extends BaseController {

  @Resource
  private EngineService engineService;

  @Resource
  private CheckService checkService;


  @PreAuthorize("@ss.hasPermi('engine:list')")
  @GetMapping("/getEngineByPage")
  public TableDataInfo getEngineByPage(EngineRequestDTO request) {
    startPage(request.getPageNum(), request.getPageSize());
    return getDataTable(engineService.getEngineByPage(request));
  }


  @PreAuthorize("@ss.hasPermi('engine:list')")
  @GetMapping("/getEngineByCode")
  public AjaxResult getEngineByCode(EngineRequestDTO request) {
    return success(engineService.getEngineByCode(request.getCode()));
  }


  @PreAuthorize("@ss.hasPermi('engine:option')")
  @GetMapping("/getEngineForOption")
  public TableDataInfo getEngineForOption(EngineRequestDTO request) {
    return getDataTable(engineService.getEngineForOption(request));
  }

  @Log(title = "引擎管理", businessType = BusinessType.INSERT)
  @PostMapping("/addEngine")
  public AjaxResult addEngine(@Validated @RequestBody EngineRequestDTO request) {
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    engineService.addEngine(request);
    return success();
  }

  @Log(title = "引擎管理", businessType = BusinessType.UPDATE)
  @PutMapping("/modifyEngine")
  public AjaxResult modifyEngine(@Validated @RequestBody EngineRequestDTO request) {
    checkService.check("data_engine", request.getCode());
    request.setOperationBy(SecurityUtils.getLoginUser().getUserId());
    engineService.modifyEngine(request);
    return success();
  }

  @Log(title = "引擎管理", businessType = BusinessType.DELETE)
  @DeleteMapping("/deleteEngine/{engineCode}")
  public AjaxResult deleteEngine(@PathVariable String engineCode) {
    checkService.check("data_engine", engineCode);
    engineService.deleteEngine(engineCode, SecurityUtils.getLoginUser().getUserId());
    return success();
  }



}
